﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using cpe.cpein.DATA;

namespace cpe.cpein.BUSINESS
{
    public static class BUUserToGroup
    {
        /// <summary>
        /// Get All the UserToGroup of the database.
        /// </summary>
        /// <param name="searchInDeleted"></param>
        /// <returns></returns>
        public static List<UserToGroup> GetAllUserToGroup(bool searchInDeleted)
        {
            List<UserToGroup> UserToGroups = new List<UserToGroup>();
            using (cpeinEntities db = new cpeinEntities())
            {
                var results = from UserToGroup in db.UserToGroup
                              where UserToGroup.utg_isdeleted == searchInDeleted
                              select UserToGroup;
                foreach (UserToGroup tmp in results)
                    UserToGroups.Add(tmp);
            }

            return UserToGroups;
        }

        /// <summary>
        /// Get the UserToGroup by id.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static UserToGroup GetUserToGroupByID(int id)
        {
            if (id <= 0)
                return null;
            else
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    var utg = from UserToGroup in db.UserToGroup
                              where UserToGroup.utg_id == id
                              select UserToGroup;
                    foreach (UserToGroup tmp in utg)
                        return tmp;
                    return null;
                }
            }
        }

        /// <summary>
        /// Add a UserToGroup into the database.
        /// </summary>
        /// <param name="utg"></param>
        /// <returns></returns>
        public static UserToGroup UserToGroupAdd(UserToGroup utg)
        {
            if (utg != null)
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    UserToGroup nutg = db.UserToGroup.Add(utg);
                    db.SaveChanges();
                    return nutg;
                }
            }
            else
                return null;
        }

        /// <summary>
        /// Remove a specific UserToGroup of the database.
        /// </summary>
        /// <param name="utg"></param>
        /// <returns></returns>
        public static bool RemoveUserToGroup(UserToGroup utg)
        {
            if (utg != null)
            {
                utg.utg_isdeleted = true;
                if (UpdateUserToGroup(utg) != null)
                {
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// Update the UserToGroup into the database.
        /// </summary>
        /// <param name="utg"></param>
        /// <returns></returns>
        public static UserToGroup UpdateUserToGroup(UserToGroup utg)
        {
            if (utg != null)
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    var current = from UserToGroup in db.UserToGroup
                                  where UserToGroup.utg_id == utg.utg_id
                                  select UserToGroup;

                    foreach (UserToGroup tmp in current.ToList())
                    {
                        tmp.grp_id = utg.grp_id;
                        tmp.usr_id = utg.usr_id;
                        tmp.utg_role = utg.utg_role;
                        tmp.utg_date_to = utg.utg_date_to;
                        tmp.utg_date_from = utg.utg_date_from;
                        tmp.std_study_id = utg.std_study_id;
                        tmp.utg_isdeleted = utg.utg_isdeleted;

                        db.SaveChanges();
                    }

                    utg = current as UserToGroup;
                }
            }

            return utg;
        }
    }
}
